#include string_helpers.inc;
function mySQLHandlerClass(strHost, intPort)
{
	this.socket = new TSocket('tcp');
	this.strHost = strHost;
	this.socket.host = strHost;
	this.socket.port = intPort;
	this.socket.timeout = 4;
	this.pkt_hdr = strFromRawData(
			0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	);
	this.pkt_pt2 = strFromRawData (
			0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x61, 0x30, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x20, 0x18, 0x81, 0xb8, 0x2c, 0x08, 0x03,
			0x01, 0x06, 0x0a, 0x09, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x73, 0x71, 0x75, 0x65, 0x6c, 0x64, 0x61,
			0x20, 0x31, 0x2e, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00
	);
	this.pkt_pt3 = strFromRawData (
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x04, 0x02, 0x00, 0x00, 0x4d, 0x53, 0x44,
			0x42, 0x4c, 0x49, 0x42, 0x00, 0x00, 0x00, 0x07, 0x06, 0x00, 0x00,
			0x00, 0x00, 0x0d, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	);
	this.pkt_lang = strFromRawData(
			0x02, 0x01, 0x00, 0x47, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x30, 0x30, 0x00, 0x00,
			0x00, 0x03, 0x00, 0x00, 0x00
	);
}
mySQLHandlerClass.prototype.generateLoginPacket = function (strUsr, strPas)
{
    var intUsrLen = strUsr.length;
		var intPasLen = strPas.length;
		var usrPad = 30 - intUsrLen;
		var pasPad = 30 - intPasLen;
        
    var ubuf = "";
    var pbuf = "";
		var ubuf, pbuf, ublen, pblen;
    
    var strNull = strFromRawData(0x00);
	  
  
    if(intUsrLen > 0)
        ublen = strFromRawData(intUsrLen % 255);
			else
        ublen = strFromRawData(0x00);
    
    
    if(intPasLen > 0)
        pblen =  strFromRawData(intPasLen % 255);
			else
        pblen = strFromRawData(0x00); 
    ubuf = strFromData(["string", strUsr], ["fill", usrPad, 0]);
    pbuf = strFromData(["string", strPas], ["fill", pasPad, 0]);
		packet = this.pkt_hdr + ubuf + ublen + pbuf + pblen + this.pkt_pt2 + pblen + pbuf + this.pkt_pt3;
    return(packet);
}
mySQLHandlerClass.prototype.receive = function()
{
	var data = "";
	var head = this.socket.ReceiveBytes(4);
	if(head.length == 4)
	{
		var intSize = head.getWordAt(2);
		data = this.socket.ReceiveBytes(intSize - 4);
	
		if((head + data).length != intSize)
		{
			trace("msSqlReceive error" );
			return("");
		}
		
		trace("msSqlReceive (" + intSize + "): " + (head + data).toHexString() );
	}
	else
	{
		trace("msSqlReceive error" );
		return("");
	}
	return(head + data);
}
mySQLHandlerClass.prototype.login = function(strUserName, strPassword)
{
	this.socket.connect();
	if(this.socket.IsConnected)
	{
		this.socket.send(this.generateLoginPacket(strUserName, strPassword));
		this.socket.send(this.pkt_lang);
		var data = this.receive();
		this.socket.Close();
		if(data.length > 0)
		{
			trace(data.toReadableString());
			if((data.length > 10) && (data.getByteAt(8) == 0xE3))
			{
				trace("Login OK! Username: " + strUserName + " Password: " + strPassword);
				return(1);
			}
			else
			{
				Trace("Login failed!");
				return(0);
			}
		}
		else
		{
			return(-1);
		}
	}
	else
	{
		Trace("Connection refused.");
		return(-2);
	}
	return(false);
}
mySQLHandlerClass.prototype.ping = function(strHost, intPort)
{
	var soc = new TSocket('udp');
	soc.host = strHost;
	soc.port = 1434;
	soc.timeout = 5;
	soc.connect();
	if(soc.IsConnected)
	{
		soc.send(strFromRawData(0x02));
		
		var data = soc.receive(true);
		soc.Close();
		
		if(data.length > 0)
		{
			//Trace("Server responded: " + data);
			var match;
			var info = new Object();
			match = data.match(/ServerName;(.*?);/);
			info.ServerName = ""; if(match) info.ServerName = match[1];
			match = data.match(/InstanceName;(.*?);/);
			info.InstanceName = ""; if(match) info.InstanceName = match[1];
			match = data.match(/IsClustered;(.*?);/);
			info.IsClustered = ""; if(match) info.IsClustered = match[1];
			match = data.match(/Version;(.*?);/);
			info.Version = ""; if(match) info.Version = match[1];
			match = data.match(/TCPPort;(.*?);/);
			info.TCPPort = ""; if(match) info.TCPPort = match[1];
			
			return(info);
		}
	}
	return(null);
}
